package com.ruoyi.framework.config;

import com.ruoyi.project.system.domain.SysGoods;
import com.ruoyi.project.system.domain.SysGoodsOrder;
import com.ruoyi.project.system.domain.SysReceiveCoupon;
import com.ruoyi.project.system.domain.SysServiceOrder;
import com.ruoyi.project.system.service.ISysGoodsOrderService;
import com.ruoyi.project.system.service.ISysGoodsService;
import com.ruoyi.project.system.service.ISysReceiveCouponService;
import com.ruoyi.project.system.service.ISysServiceOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 监听所有db的过期事件__keyevent@*__:expired"
 */
@Component
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {

    @Autowired
    private ISysServiceOrderService sysServiceOrderService;

    @Autowired
    private ISysGoodsOrderService sysGoodsOrderService;

    @Autowired
    private ISysReceiveCouponService sysReceiveCouponService;


    @Autowired
    private ISysGoodsService sysGoodsService;

    public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }
    /**
     * 针对 redis 数据失效事件，进行数据处理
     * @param message
     * @param pattern
     */
    @Override
    @Transactional
    public void onMessage(Message message, byte[] pattern) {
        // 获取到失效的 key，进行取消订单业务处理
        String expiredKey = message.toString();
        if(expiredKey.startsWith("S")&&expiredKey.length()==14){
            SysServiceOrder sysServiceOrder=sysServiceOrderService.selectSysServiceOrderByCode(expiredKey);
            if(null!=sysServiceOrder){
                if("0".equals(sysServiceOrder.getOrderStatus())){
                    sysServiceOrder.setOrderStatus("4");
                    //取消订单时判断优惠券是否使用优惠券，如果使用则把优惠券退回给用户
                    if(null!=sysServiceOrder.getCouponId()){
                        SysReceiveCoupon sysReceiveCoupon=new SysReceiveCoupon();
                        sysReceiveCoupon.setCouponId(sysServiceOrder.getCouponId());
                        sysReceiveCoupon.setMobileUserId(sysServiceOrder.getMobileUserId());
                        sysReceiveCoupon.setUsed("1");
                        List<SysReceiveCoupon> list=sysReceiveCouponService.selectSysReceiveCouponList(sysReceiveCoupon);
                        if(!list.isEmpty()){
                            SysReceiveCoupon receiveCoupon=list.get(0);
                            receiveCoupon.setUsed("0");
                            sysReceiveCouponService.updateSysReceiveCoupon(receiveCoupon);
                        }
                    }
                    sysServiceOrderService.updateSysServiceOrder(sysServiceOrder);
                }
            }
        }
        if(expiredKey.startsWith("G")&&expiredKey.length()==14){
            SysGoodsOrder sysGoodsOrder=sysGoodsOrderService.selectSysGoodsOrderByNo(expiredKey);
            if(null!=sysGoodsOrder){
                if("0".equals(sysGoodsOrder.getPayStatus())&&"0".equals(sysGoodsOrder.getGoodsOrderStatus())){
                    sysGoodsOrder.setGoodsOrderStatus("1");
                    sysGoodsOrderService.updateSysGoodsOrder(sysGoodsOrder);
                    SysGoods sysGoods=new SysGoods();
                    sysGoods.setGoodsId(sysGoodsOrder.getGoodsId());
                    sysGoods.setItemStatus("1");
                    sysGoods.setUpdateTime(new Date());
                    sysGoodsService.updateSysGoods(sysGoods);
                }
            }
        }
        System.out.println(expiredKey);
    }

}
